home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / netlib / serveraccept.c < prev    next >
C/C++ Source or Header  |  1994-04-12  |  4KB  |  160 lines

  1. RCS_ID_C="$Id: serveraccept.c,v 1.3 1994/04/12 20:45:08 jraja Exp $";
  2. /*
  3.  * Author: ppessi <Pekka.Pessi@hut.fi>
  4.  *
  5.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  6.  *                  Helsinki University of Technology, Finland.
  7.  *                  All rights reserved.
  8.  *
  9.  * Created      : Tue May 25 19:30:13 1993 ppessi
  10.  * Last modified: Fri Oct 15 00:50:51 1993 ppessi
  11.  * 
  12.  * $Log: serveraccept.c,v $
  13.  * Revision 1.3  1994/04/12  20:45:08  jraja
  14.  * updated the autodoc.
  15.  *
  16.  * Revision 1.2  1993/10/15  01:15:36  ppessi
  17.  * Use now StrToLong()
  18.  *
  19.  * Revision 1.1  1993/06/03  23:27:19  ppessi
  20.  * Cosmetic changes for version 1.0
  21.  *
  22.  */
  23.  
  24. /****** net.lib/serveraccept ***********************************************
  25.  
  26.     NAME
  27.         serveraccept - Accept a server connection on named port
  28.  
  29.     SYNOPSIS
  30.         socket = serveraccept(name, peer);
  31.  
  32.         long serveraccept(char *, struct sockaddr_in *);
  33.  
  34.     DESCRIPTION
  35.         The serveraccept() library call binds a socket to the named Internet
  36.         TCP port. Then it listens the socket and accepts the connection to
  37.         the port. The peer's socket address is returned in sockaddr pointed
  38.         by sockaddr argument.
  39.  
  40.         The port name is resolved by getservbyname() call. A numeric value
  41.         for port name is also accepted.
  42.   
  43.         This module is meant for daemon developing.
  44.  
  45.     INPUTS
  46.         name   - port name or numeric string.
  47.         peer   - pointer to struct sockaddr_in
  48.  
  49.     RESULT
  50.         socket - positive socket id for success or -1 for failure.
  51.  
  52.         peer   - sockaddr_in structure containing peer's internet address.
  53.                  Note that on error, the structure containing peer address
  54.                  is not necessarily updated.
  55.  
  56.     AUTHOR
  57.         Pekka Pessi, the AmiTCP/IP Group, <amitcp-group@hut.fi>,
  58.         Helsinki University of Technology, Finland.
  59.  
  60.     SEE ALSO
  61.         bsdsocket/accept, bsdsocket/getservbyname
  62.  
  63. *****************************************************************************
  64. *
  65. */
  66.  
  67. #ifdef AMIGA
  68. #if __SASC
  69. #include <proto/socket.h>
  70. #include <proto/dos.h>
  71. #include <clib/exec_protos.h>
  72. #include <pragmas/exec_sysbase_pragmas.h>
  73. #elif __GNUC__
  74. #include <inline/socket.h>
  75. #include <inline/exec.h>
  76. #else
  77. #include <clib/socket_protos.h>
  78. #endif
  79. #endif /* AMIGA */
  80.  
  81. #include <errno.h>
  82. #include <netdb.h>
  83.  
  84. #include <sys/param.h>
  85. #include <sys/socket.h>
  86. #include <sys/ioctl.h>
  87. #include <netinet/in.h>
  88.  
  89. #include <signal.h>
  90.  
  91. #include <dos/dos.h>
  92. #include <exec/execbase.h>
  93. #include <dos/var.h>
  94.  
  95. #include <stdlib.h>
  96.  
  97. /*
  98.  * serveraccept:
  99.  *      Accept a server socket from the named port
  100.  */
  101. long
  102. serveraccept(char *pname, struct sockaddr_in *ha)
  103. {
  104.   struct sockaddr_in sin; 
  105.   long ha_len = sizeof(*ha);
  106.   int s, sa;
  107.   long port;
  108.   struct servent *sp;
  109.   long on = 1;
  110.  
  111.   /* Create address corresponding our service */
  112.   bzero((caddr_t)&sin, sizeof(sin));
  113. #ifdef BSD4_4
  114.   sin.sin_len = sizeof(struct sockaddr_in);
  115. #endif
  116.   sin.sin_family = AF_INET;
  117.  
  118.   /* A port must be in the range 1 - 65535 */
  119.   if (StrToLong(pname, &port) > 0 && port < 65536 )
  120.     sin.sin_port = port;
  121.   else if (sp = getservbyname(pname, "tcp"))
  122.     sin.sin_port = sp->s_port;
  123.   else {
  124.     return -1;
  125.   }
  126.  
  127.   sin.sin_addr.s_addr = INADDR_ANY;
  128.  
  129.   if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  130.     PrintNetFault(Errno(), "socket");
  131.     return -1;
  132.   }
  133.  
  134.   /* Reuse this port */
  135.   if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) {
  136.     PrintNetFault(Errno(), "setsockopt");
  137.     sa = -1; goto Return;
  138.   }
  139.  
  140.   /* Bind it to socket */
  141.   if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0 ) {
  142.     PrintNetFault(Errno(), "bind");
  143.     sa = -1; goto Return;
  144.   }
  145.  
  146.   if (listen(s, 1) < 0) {
  147.     PrintNetFault(Errno(), "listen");
  148.     sa = -1; goto Return;
  149.   }
  150.  
  151.   if ((sa = accept(s, (struct sockaddr *)ha, &ha_len)) < 0){
  152.     PrintNetFault(Errno(), "accept");
  153.   }
  154.  
  155.  Return:
  156.   CloseSocket(s);
  157.   return sa;
  158. }
  159.  
  160.